package com.leyou.search.web;

import com.leyou.search.entity.Goods;
import com.leyou.search.entity.SearchParamDTO;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/init")
    public ResponseEntity<String>init(){
        //初始话索引
        searchService.createIndexAndMapping();
        //加载数据
        searchService.loadData();
        return ResponseEntity.ok("初始话索引库成功");
    }

    /**
     * 关键字自动补全
     * @param keyPrefix
     * @return
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key")String keyPrefix){
        return searchService.getSuggestion(keyPrefix);
    }

    /**
     * 搜索商品列表
     * paramDTO 请求参数的form表单
     * @return 分页的商品数据
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>>searchGoodList(@RequestBody SearchParamDTO paramDTO){
        return searchService.searchGoodList(paramDTO);
    }
}
